/*************************************
Copyright 2011 Chinua Iloabachie, Pradnya Khutafale, Siyi Liu, Junichi Suzuki, and The University of Massachusetts, Boston (http://code.google.com/p/tabletuml/)


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 
*************************************/



package touchMe.Draw;

import org.apache.commons.lang.StringEscapeUtils;

import touchMe.Help.WebHelp;
import touchMe.baseClass.Constants;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class PropertyBox extends Dialog{
	private View propertybox;
	private EditText editor;
	private String methods;
	private TouchMe touchMe;
	Context context;
	String elementId;
	int mode=Constants.NEW;
	String elementType;
	public PropertyBox(Context context, String methods, int mode,String elementId,String type) {
		super(context);
		this.methods=StringEscapeUtils.unescapeHtml(methods);
		this.context=context;
		this.touchMe=(TouchMe)context;
		this.mode=mode;
		this.elementId=elementId;
		this.elementType=type;
	}
	@Override
	protected void onCreate(Bundle savedInstanceSate){
		super.onCreate(savedInstanceSate);
		
		setTitle(R.string.properties);
		
		
		setContentView(R.layout.propertybox);
		editor=(EditText)findViewById(R.id.pb_editor);
		editor.setText(getMethods());
		propertybox=findViewById(R.id.propertybox);
		if(mode==Constants.NEW){
			View delete=findViewById(R.id.pb_delete);
			delete.setVisibility(View.INVISIBLE);
		}
		
				
		propertybox.setOnClickListener( new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				return;
				//returnResult(0);
			}
		});
		View pb_help=findViewById(R.id.pb_help);
		pb_help.setOnClickListener( new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d(Constants.TAG,"processBox 00");
				processHelp();
			}
		});
		View pb_cancel=findViewById(R.id.pb_cancel);
		pb_cancel.setOnClickListener( new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		View pb_ok=findViewById(R.id.pb_ok);
		pb_ok.setOnClickListener( new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d(Constants.TAG,"processBox 222");
				processBox(2,elementType);
			}
		});
		View pb_delete=findViewById(R.id.pb_delete);
		pb_delete.setOnClickListener( new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d(Constants.TAG,"processBox 333");
				processBox(Constants.DELETEELEMENT,elementType);
			}
		});
		// Override  for Inheritance /Implementation relation
		if(elementType.equals("INHERI_IMPLEMENT"))
		{
			setTitle("Inherit/Implement Relation");
			editor.setVisibility(View.INVISIBLE);
			pb_ok.setVisibility(View.INVISIBLE);
		}
	}
	public String getMethods() {
		return methods;
	}
	public void setMethods(String methods) {
		this.methods = methods;
	}
	public void processHelp(){
		Log.d(Constants.TAG,"Help for "+elementType);
		touchMe.showHelp(elementType);	
	}
	public void processBox(int state,String elementType){
		if(mode==Constants.NEW){
			if(state==0){
				showMsg("show some help hia",1);
				dismiss();
			}
			if(state==1) dismiss();
			if(state==2){
				if(editor.getText().length()>1){
					Log.d(Constants.TAG,"New class diagram");
					if(elementType.equals(Constants.classType)){
						touchMe.createNewModell(Constants.classType, StringEscapeUtils.escapeHtml(editor.getText().toString()),Constants.ADDELEMENT);						
					} 
					else if(elementType.equals(Constants.usecaseType))
						touchMe.createNewModell(Constants.usecaseType, StringEscapeUtils.escapeHtml(editor.getText().toString()),Constants.ADDELEMENT);
					else if(elementType.equals(Constants.relationType)){
						touchMe.createNewModell(Constants.relationType, StringEscapeUtils.escapeHtml(editor.getText().toString()), Constants.ADDELEMENT);
					}
					else if(elementType.equals(Constants.actorType))
						touchMe.createNewModell(Constants.actorType, StringEscapeUtils.escapeHtml(editor.getText().toString()),Constants.ADDELEMENT);
					 
					dismiss();
				}
				else{
					showMsg("Please edit properties",1);
				}
			}
		}
		else if(mode==1){
			if(state==0){
				//Intent helpIntent=new Intent(PropertyBox.this, WebHelp.class);
	        	//startActivity(helpIntent);
				//showMsg("show some help hia",1);
				dismiss();
			}
			if(state==1) dismiss();
			if(state==Constants.UPDATEELEMENT){
				if(editor.getText().length()>1){
					Log.d(Constants.TAG,"Edit class diagram");
					touchMe.updateDeleteModell(elementId, StringEscapeUtils.escapeHtml(editor.getText().toString()), Constants.UPDATEELEMENT);
					dismiss();
				}
				else{
					showMsg("Please edit properties",1);
				}
			}
			if(state==Constants.DELETEELEMENT){
				Log.d(Constants.TAG,"Delete class diagram");
				touchMe.updateDeleteModell(elementId, StringEscapeUtils.escapeHtml(editor.getText().toString()), Constants.DELETEELEMENT);
				dismiss();
			}
		}
	}
	public void showMsg(String msg, int howLong){
		if(howLong==0)
			howLong=2000;
		if(howLong==1)
			howLong=3000;
		else
			howLong=4000;
		Toast.makeText(context, msg, howLong).show();
		Log.d(Constants.TAG,"showMsg "+howLong);
	}
	
	
}
